<?php


class Blackbird_UsersController extends Zend_Controller_Action
{
	function requireLogin()
	{
		$auth = Zend_Auth::getInstance();
		$request = $this->getRequest();
		if (!$auth->hasIdentity())
		{
			return $this->_redirect(BASE_URL . '/users/login?redirectUrl=' . $request->getRequestUri(),
				array('code' => 303));
		}
	}

	function indexAction()
	{
		$this->_forward('login');
	}

	function signupAction()
	{
		$request = $this->getRequest();
		$form = new Blackbird_Form_Users_SignUp();
		if ($request->isPost() && $form->isValid($request->getPost()))
		{
			$model = new Blackbird_Model_Users();
			if ($model->signup($form) !== false)
				return $this->_redirect(BASE_URL . '/users/activate', array('code' => 303));
		}
		$this->view->form = $form;
		$this->view->flashMessages = $this->_helper->getHelper('FlashMessenger')->getMessages();
	}

	function activateAction()
	{
		$request = $this->getRequest();
		$form = new Blackbird_Form_Users_Activate();
		if (count($request->getQuery()) > 0 && $form->isValid($request->getQuery()))
		{
			$model = new Blackbird_Model_Users();
			if ($model->activate($form) !== false)
			{
				$this->_helper->getHelper('FlashMessenger')
					->addMessage('The account has been activated.');
				return $this->_redirect(BASE_URL . '/users/activated', array('code' => 303));
			}
		}
		$this->view->form = $form;
		$this->view->flashMessages = $this->_helper->getHelper('FlashMessenger')->getMessages();
	}

	function activatedAction()
	{
		$form = new Blackbird_Form_Users_Login();
		$form->setAction(BASE_URL . '/users/login');
		$this->view->form = $form;
		$this->view->flashMessages = $this->_helper->getHelper('FlashMessenger')->getMessages();
	}

	function loginAction()
	{
		$auth = Zend_Auth::getInstance();
		if ($auth->hasIdentity())
			return $this->_redirect(BASE_URL . '/users/profile', array('code' => 303));

		$request = $this->getRequest();
		$form = new Blackbird_Form_Users_Login();
		$form->setDefaults(array('redirectUrl' => $this->_getParam('redirectUrl')));
		if ($request->isPost() && $form->isValid($request->getPost()))
		{
			$model = new Blackbird_Model_Users();
			if ($model->login($form) !== false)
			{
				$redirectUrl = $form->getValue('redirectUrl');
				if ('' == $redirectUrl)
					$redirectUrl = BASE_URL;
				return $this->_redirect($redirectUrl, array('code' => 303));
			}
		}
		$this->view->form = $form;
		$this->view->flashMessages = $this->_helper->getHelper('FlashMessenger')->getMessages();
	}

	function profileAction()
	{
		$this->requireLogin();

		$model = new Blackbird_Model_Users();
		$user = $model->fetchCurrentUser();
		$values = get_object_vars($user);
		$values['birthDate'] = substr($values['birthDate'], 0, 10);

		$request = $this->getRequest();
		$form = new Blackbird_Form_Users_ChangeProfile();
		$form->setDefaults($values);
		if ($request->isPost() && $form->isValid($request->getPost()))
		{
			if ($model->changeProfile($form))
			{
				$this->_helper->getHelper('FlashMessenger')
					->addMessage('The profile has been modified.');
				return $this->_redirect(BASE_URL . '/users/profile', array('code' => 303));
			}
		}

		$this->view->form = $form;
		$this->view->user = $user;
		$this->view->flashMessages = $this->_helper->getHelper('FlashMessenger')->getMessages();
	}

	function changePasswordAction()
	{
		$this->requireLogin();

		$request = $this->getRequest();
		$form = new Blackbird_Form_Users_ChangePassword();
		$model = new Blackbird_Model_Users();
		if ($request->isPost())
		{
			if ($form->isValid($request->getPost()) && $model->changePassword($form))
			{
				$this->_helper->getHelper('FlashMessenger')
					->addMessage('The password has been modified.');
				return $this->_redirect(BASE_URL . '/users/profile', array('code' => 303));
			}
		}
		$this->view->user = $model->fetchCurrentUser();
		$this->view->form = $form;
		$this->view->flashMessages = $this->_helper->getHelper('FlashMessenger')->getMessages();
	}

	function changePictureAction()
	{
		$this->requireLogin();

		$request = $this->getRequest();
		$form = new Blackbird_Form_Users_ChangePicture();
		$model = new Blackbird_Model_Users();
		if ($request->isPost() && $form->isValid($request->getPost()))
		{
			if ($model->changePicture($form))
			{
				$this->_helper->getHelper('FlashMessenger')
					->addMessage('The picture has been modified.');
				return $this->_redirect(BASE_URL . '/users/profile', array('code' => 303));
			}
		}
		$this->view->user = $model->fetchCurrentUser();
		$this->view->form = $form;
		$this->view->flashMessages = $this->_helper->getHelper('FlashMessenger')->getMessages();
	}

	function useGravatarAction()
	{
		$this->requireLogin();

		$model = new Blackbird_Model_Users();
		$model->useGravatar();
		$this->_helper->getHelper('FlashMessenger')
			->addMessage('The picture has been modified.');
		return $this->_redirect(BASE_URL . '/users/profile', array('code' => 303));
	}

	function logoutAction()
	{
		$model = new Blackbird_Model_Users();
		$model->logout();
		$this->_redirect(BASE_URL, array('code' => 303));
	}

	function forgotPasswordAction()
	{
		$form = new Blackbird_Form_Users_ForgotPassword();
		$request = $this->getRequest();
		if ($request->isPost() && $form->isValid($request->getPost()))
		{
			$model = new Blackbird_Model_Users();
			if ($model->forgotPassword($form))
			{
				$this->_helper->getHelper('FlashMessenger')
					->addMessage('An email with instructions to recover the password has been sent.');
				return $this->_redirect(BASE_URL . '/users/recover-password', array('code' => 303));
			}
		}
		$this->view->form = $form;
		$this->view->flashMessages = $this->_helper->getHelper('FlashMessenger')->getMessages();
	}

	function recoverPasswordAction()
	{
		$form = new Blackbird_Form_Users_RecoverPassword();
		$request = $this->getRequest();
		if (count($request->getQuery()) > 0 && $form->isValid($request->getQuery()))
		{
			$model = new Blackbird_Model_Users();
			if ($model->recoverPassword($form))
			{
				$this->_helper->getHelper('FlashMessenger')
					->addMessage('The new password has been sent to the account\'s email.');
				return $this->_redirect(BASE_URL . '/users/password-recovered', array('code' => 303));
			}
		}
		$this->view->form = $form;
		$this->view->flashMessages = $this->_helper->getHelper('FlashMessenger')->getMessages();
	}

	function passwordRecoveredAction()
	{
		$form = new Blackbird_Form_Users_Login();
		$form->setAction(BASE_URL . '/users/login');
		$this->view->form = $form;
		$this->view->flashMessages = $this->_helper->getHelper('FlashMessenger')->getMessages();
	}
}

